package educar.models;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import educar.db.DbConnection;
import educar.db.JPA;


public class facultadCargo {
	private static JPA jpa = new JPA();
	private Integer codigo;
	private Integer id;
	
	public facultadCargo (Integer id,Integer codigo){
		this.setCodigo(codigo);
		this.setId(id);
	}
	
	
	public boolean save() {
		try {
			facultadCargo.getfacultadCargoByfacultadCargo(Integer.toString(id),Integer.toString(codigo));
			return false;
		} catch (datoNotFound e1) {
			String[] columns = {"id","codigo"};

			PreparedStatement stm = jpa.newRecord("educar_dev.facultadCargo", columns);

			try {
				stm.setString(1, Integer.toString(id));
				stm.setString(2, Integer.toString(codigo));
				
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}

			jpa.create(stm);
			return true;
		}
	}
	
	public static facultadCargo getfacultadCargoByfacultadCargo(String id,String codigo) throws datoNotFound {
		ResultSet rs = null;
		ResultSet rs2 = null;
		facultadCargo p = null;

		rs = jpa.getByField("facultadCargo", "codigo", codigo);
		rs2=jpa.getByField("facultadCargo", "id", id);
		try {
			if (rs.next() && rs2.next()) {
				p = new facultadCargo( Integer.parseInt(rs2.getString(2)),Integer.parseInt(rs.getString(2)));
			} else {
				throw new datoNotFound();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return p;
	}

	public void destroy(String id,String codigo) {
		String query= "DELETE from " + DbConnection.bd + ".facultadCargo"; 
    	query += " Where dni= '" + id + "' and codigo= '"+codigo+"'";
		jpa.runUpdate(query);
	}

	
	public void setCodigo(Integer codigo){
		this.codigo=codigo;
	}
	
	public Integer getCodigo(){
		return this.codigo;
	}
	
	public void setId(Integer id){
		this.id=id;
	}
	
	public Integer getId(){
		return this.id;
	}
}

